﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input.EarTagInput;
using IntelligentAgriculture.Contracts.Dto.Output.EarTagOutput;
using IntelligentAgriculture.Contracts.Dto.Output.PageOutput;
using IntelligentAgriculture.Contracts.Dto.Output.RidgepOutPut;
using IntelligentAgriculture.Contracts.Interface.EarTagService;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Impl.EarTagReposiory;
using IntelligentAgriculture.Domain.Interface.IEarTagReposiotry;
using Microsoft.EntityFrameworkCore;
using Pinyin4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;


namespace IntelligentAgriculture.Services.EarTageService
{
    public class EarTageService:IEarTageService
    {
        private readonly IEarTagReposiotry _iearTagReposiotry;

        private readonly IMapper _mapper;
        public EarTageService(IEarTagReposiotry iearTagReposiotry,  IMapper mapper)
        {
            _iearTagReposiotry = iearTagReposiotry;
            _mapper = mapper;
        }
 
        



        //public async Task<int> Add(EarTagsInput entity)
        //{
        //    try
        //    {

        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    } 
        //}


        ////获取首字母
        //static public string GetPinyinFirstChar(string strText)
        //{
        //    var cont = "";
        //    foreach (var character in strText)
        //    {
        //        var results = PinyinHelper.ToHanyuPinyinStringArray(character);
        //        if (results != null && results.Length > 0)
        //        {
        //            var firstLetter = results[0][0].ToString().ToUpper();
        //            cont += firstLetter;
        //        }
        //    }
        //    return cont;
        //}

        /// <summary>
        /// 显示，查询，分页功能
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>列表</returns>
        public async Task<PagesOutput<EarTageShowsOutPut>> GetShow(EarTageShowInput input)
        {
            var list = await _iearTagReposiotry.GetShow();
            if(!string.IsNullOrEmpty(input.EarTagNumber))
            {
                list = list.Where(x => x.EarTagNumber.Contains(input.EarTagNumber));
            }
            if(input.EarTagBreeds>0)
            {
                list = list.Where(x => x.EarTagBreeds == input.EarTagBreeds);
            }
            if(input.EarTagBirthStatus>0)
            {
                list = list.Where(x => x.EarTagBirthStatus == input.EarTagBirthStatus);
            }
            if(input.EarTagUseStatus>0)
            {
                list = list.Where(x => x.EarTagUseStatus == input.EarTagUseStatus);
            }
            if(input.EarTagStatus>0)
            {
                list = list.Where(x => x.EarTagStatus == 0);
            }
            
            
            var result = new PagesOutput<EarTageShowsOutPut>()
            {
                Total = list.Count(),
                PageSize = input.PageSize
            };
            
            var query = list.OrderByDescending(x=>x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = _mapper.Map<List<EarTageShowsOutPut>>(query);
            return result;
        }

        /// <summary>
        /// 根据指定的ID删除单个耳标信息
        /// </summary>
        /// <param name="id">要删除的耳标信息的ID</param>
        /// <returns>返回一个表示操作成功的异步任务，返回删除的记录数</returns>
        public async  Task<int> Delete(int id)
        {
            try
            {
                return await _iearTagReposiotry.Delete(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除指定IDs的耳标信息
        /// </summary>
        /// <param name="ids">要删除的耳标信息的ID集合。</param>
        /// <returns>返回一个表示操作成功的异步任务，返回删除的记录数。</returns>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            return await _iearTagReposiotry.DeleteRange(ids);
        }

        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<int> Add(Eartag entity)
        {
            var Eartags = new List<Eartag>();

            if (string.IsNullOrEmpty(entity.BeforNumber))
            {
                // 1杜泊 2绒山  3美利奴 4黄淮山 5南江黄 6马头山 7黑山
                switch (entity.EarTagBreeds)
                {

                    case 1:
                        entity.EarTagNumber = "DB-" + entity.EarTagNumber;
                        break;
                    case 2:
                        entity.EarTagNumber = "AZB-" + entity.EarTagNumber;
                        break;
                    case 3:
                        entity.EarTagNumber = "SFK-" + entity.EarTagNumber;
                        break;
                    case 4:
                        entity.EarTagNumber = "DH-" + entity.EarTagNumber;
                        break;
                    case 5:
                        entity.EarTagNumber = "DDH-" + entity.EarTagNumber;
                        break;
                    case 6:
                        entity.EarTagNumber = "SDH-" + entity.EarTagNumber;
                        break;
                    case 7:
                        entity.EarTagNumber = "AH-" + entity.EarTagNumber;
                        break;
                    case 8:
                        entity.EarTagNumber = "HY-" + entity.EarTagNumber;
                        break;
                    case 9:
                        entity.EarTagNumber = "ADH-" + entity.EarTagNumber;
                        break;
                    default:
                        entity.EarTagNumber = entity.EarTagNumber + "";
                        throw new Exception();
                }
                var list = await _iearTagReposiotry.GetShow(); // 等待异步操作完成并获取耳标列表

                // 使用 LINQ 查询查找耳标信息
                var existingEartag = list.FirstOrDefault(c => c.EarTagNumber == entity.EarTagNumber);

                // 如果 existingEartag 不为 null，则表示耳标已存在，返回 -1
                if (existingEartag != null)
                {
                    return -1;
                }

                // 否则，耳标不存在，可以将 entity 添加到 Eartags 中
                Eartags.Add(_mapper.Map<Eartag>(entity));
            }
            else
            {

                switch (entity.EarTagBreeds)
                {

                    case 1:
                        entity.EarTagNumber = "DB-" + entity.EarTagNumber;
                        break;
                    case 2:
                        entity.EarTagNumber = "AZB-" + entity.EarTagNumber;
                        break;
                    case 3:
                        entity.EarTagNumber = "SFK-" + entity.EarTagNumber;
                        break;
                    case 4:
                        entity.EarTagNumber = "DH-" + entity.EarTagNumber;
                        break;
                    case 5:
                        entity.EarTagNumber = "DDH-" + entity.EarTagNumber;
                        break;
                    case 6:
                        entity.EarTagNumber = "SDH-" + entity.EarTagNumber;
                        break;
                    case 7:
                        entity.EarTagNumber = "AH-" + entity.EarTagNumber;
                        break;
                    case 8:
                        entity.EarTagNumber = "HY-" + entity.EarTagNumber;
                        break;
                    case 9:
                        entity.EarTagNumber = "ADH-" + entity.EarTagNumber;
                        break;
                    default:
                        //entity.EarTagNumber = entity.EarTagNumber + "";
                        throw new Exception();
                }
                var start = int.Parse(entity.BeforNumber);
                var end = int.Parse(entity.AfterNumber);
                for (int i = start; i <= end; i++)
                {

                    // 1杜泊 2绒山  3美利奴 4黄淮山 5南江黄 6马头山 7黑山


                    // 等待异步操作完成并获取耳标列表
                    entity.EarTagNumber = entity.EarTagNumber.Split('-')[0] + "-" + i;
                    var input = new Eartag();
                        input = entity;
                    var list = await _iearTagReposiotry.GetShow();
                    // 使用 LINQ 查询查找耳标信息
                    var existingEartag = list.FirstOrDefault(c => c.EarTagNumber == entity.EarTagNumber);

                    // 如果 existingEartag 不为 null，则表示耳标已存在，返回 -1
                    if (existingEartag != null)
                    {
                        return -1;
                    }

                    // 否则，耳标不存在，可以将 entity 添加到 Eartags 中

                   
                    Eartags.Add(_mapper.Map<Eartag>(input));

                }

            }


            return await _iearTagReposiotry.Add(Eartags);

        }
        /// <summary>
        /// 反填功能
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回列表</returns>
        public async Task<EarTageShowsOutPut> Find(int id)
        {
            try
            {
                var list = await _iearTagReposiotry.Find(id);
                if (list == null)
                {
                    return null;
                }
                var lists = _mapper.Map<EarTageShowsOutPut>(list);
                return lists;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 耳标修改功能
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>修改行数</returns>
        public async Task<int> UpateEartag(Eartag entity)
        {

            try
            {
                var list = _mapper.Map<Eartag>(entity);
                return await _iearTagReposiotry.UpdateAsync(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
		/// 显示未使用的耳标
		/// </summary>
		/// <returns></returns>
        public async Task<IQueryable<Eartag>> ShowStateEartag()
        {
            return await _iearTagReposiotry.ShowStateEartag();
        }
    }
		
		
}
